package com.cnhis.cloudhealth.clinical.hosdoctor.doctororderupdate.bo;

import com.cnhis.cloudhealth.clinical.clidoctor.clinewadvice.dao.CliNewAdviceDao;
import com.cnhis.cloudhealth.clinical.hosdoctor.doctororderupdate.dao.DoctororderupdateDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.utils.DateUtils;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DoctororderupdateBo {
	@Resource
	private CliNewAdviceDao cliNewAdviceDao;
	@Resource
	private DoctororderupdateDao doctororderupdateDao;


	private List<Map<String,String>> changeToString(List<?> list){
		List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
		for(Object o:list){
			if(o instanceof Map){
				Map<String,Object> pmap = (Map<String,Object>)o;
				Map<String,String> smap = new HashMap<String,String>();
				Iterator<Map.Entry<String, Object>> it = pmap.entrySet().iterator();
				while (it.hasNext()) {
					Map.Entry<String, Object> entry = it.next();
					String str = String.valueOf(entry.getValue());
					smap.put(entry.getKey(),str);
				}
				resultList.add(smap);
			}
		}
		return resultList;
	}

	private ModelVo packModelVo(String nid,Map map){
		ModelVo mo = new ModelVo();
		List<?> list = null;
		try {
			list = doctororderupdateDao.queryForList(nid,map);
			list = changeToString(list);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
		}
		if(list!=null&&list.size()>0) {
			mo.setList(list);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setTotal(list.size());
			mo.setRows(list);
		}
		return mo;
	}

	public ModelVo getclinicEditOrderBeforeJudge(Map map) throws Exception {
		
		ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
		
		//如果是非门诊，则返回成功
		if(!"1".equals(map.get("ACF01").toString())){
			return mo;
		}
		//是否有限制条件
		if(getBlp02(map)){
			return mo;
		}
		
		//获取限定年龄
		Integer BLP04 = getBlp04(map);
		
		//获取病人年龄
		Integer VAA10 = getVaa10(map);
		
		if((VAA10>=BLP04)&&getVcj04Vcj05(map)){
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("病人超过"+BLP04+"岁，必须要输入收缩压和舒张压.");
			return mo;
		}
		
		return mo;
	}

	/**
	 * 入收缩压和舒张压 是否为空
	 * @param map
	 * @return
	 * @throws Exception
	 */
	private boolean getVcj04Vcj05(Map map) throws Exception {
		int ct = cliNewAdviceDao.getVcj04Vcj05(map);
		if(ct>0){
			return true;
		}
		return false;
	}

	/**
	 * 获取病人年龄
	 * @param map
	 * @return
	 * @throws Exception
	 */
	private Integer getVaa10(Map map) throws Exception {
		Integer VAA10 = cliNewAdviceDao.getVaa10(map);
		VAA10 = VAA10==null?0:VAA10;
		return VAA10;
	}

	/**
	 * 获取限定年龄
	 * @param map
	 * @return
	 * @throws Exception
	 */
	private Integer getBlp04(Map map) throws Exception {
		Integer BLP04 = cliNewAdviceDao.getBlp04(map);
		BLP04 = BLP04==null?0:BLP04;
		return BLP04;
	}

	/**
	 * 是否有限制条件
	 * @param map
	 * @return
	 * @throws Exception
	 */
	private boolean getBlp02(Map map) throws Exception {
		int ct = cliNewAdviceDao.getBlp02(map);
		if(ct==0){
			return true;
		}
		return false;
	}
	
	public ModelVo checkUpdateAdvice(Map map){
		ModelVo mo = new ModelVo();
		List list = null;
		try {
		  list = doctororderupdateDao.checkUpdateAdvice(map);
		} catch (Exception e) {
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		
		mo.setRows(list);
		return mo;
	}

	public ModelVo saveCASign(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list = null;
		try {
		  list = doctororderupdateDao.saveCASign(map);
		} catch (Exception e) {
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		
		mo.setRows(list);
		return mo;
	}
	public ModelVo queryRequestExamine(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			if("1".equals(map.get("ACF01").toString())){
				list = doctororderupdateDao.queryRequestExamine1(map);
			}else{
				list = doctororderupdateDao.queryRequestExamine2(map);
			}
		} catch (Exception e) {
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		
		mo.setRows(list);
		return mo;
	}
	@SuppressWarnings("unchecked")
	public ModelVo clinicAdviceDistillUpdate(Map map) throws Exception{
		ModelVo mo = new ModelVo();
		List list = null;
		try {
		  if("1".equals(map.get("acf01").toString())&&"0".equals(map.get("lmode").toString())){
			  
		    String adate = DateUtils.getDateString(new Date());
		    Map m = getBCE02AndBCE03(map);
		   if(m!=null){
			  map.put("adate", adate);
			  map.put("bce02", m.get("bce02")==null?"":m.get("bce02").toString());
			  map.put("bce03", m.get("bce03")==null?"":m.get("bce03").toString());
		    }
		  }
		  list = doctororderupdateDao.clinicAdviceDistillUpdate(map);
		  list = changeToString(list);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		
		mo.setRows(list);
		return mo;
	}
	private Map getBCE02AndBCE03(Map map)throws Exception{
		return doctororderupdateDao.getBCE02AndBCE03(map);
	}

	public ModelVo selectPstatus(Map map){
		return packModelVo("Doctororderupdate.6907",map);
	}
	public ModelVo selectHavetransferorder(Map map){
		return packModelVo("Doctororderupdate.7695",map);
	}
	public ModelVo selectOrdermedrcd(){
		return packModelVo("Doctororderupdate.747",null);
	}
	public ModelVo selectTreamentItems(){
		return packModelVo("Doctororderupdate.1106",null);
	}
	public ModelVo selectTreamentrcd(Map map){
		return packModelVo("Doctororderupdate.1177",map);
	}
	public ModelVo selectIsanesthesiadep(Map map){
		return packModelVo("Doctororderupdate.2111",map);
	}
	public ModelVo selectIsanesthesiologist(Map map){
		return packModelVo("Doctororderupdate.2113",map);
	}
	public ModelVo selectOrdercommonitem(Map map){
		return packModelVo("Doctororderupdate.1640",map);
	}
	public ModelVo selectOrderTreamentItem(Map map){
		return packModelVo("Doctororderupdate.1047",map);
	}
	public ModelVo selectBckname(Map map){
		return packModelVo("Doctororderupdate.2103",map);
	}
	public ModelVo selectPdrugpercent(Map map){
		return packModelVo("Doctororderupdate.7316",map);
	}
	public ModelVo selectPchildbirth(Map map){
		return packModelVo("Doctororderupdate.772",map);
	}

	private String getABC02(Map map)throws Exception{
		String abc02="";
		if("1".equals(map.get("ACF01").toString())){
			abc02 = doctororderupdateDao.getABC02Param1(map);
		}else{
			abc02 = doctororderupdateDao.getABC02Param2(map);
		}
		return abc02==null?"":abc02;
	}
	private List getParam(Map map)throws Exception{
		return doctororderupdateDao.paramters(map);
	}
	private Map<String,Object> getDrugBBY25(Map map)throws Exception{
		Map<String,Object> resultMap = null;
		Integer BCK18 = (Integer)doctororderupdateDao.queryToObject("Doctororderupdate.getDrugBBY25_001",map);
		BCK18 = BCK18==null?0:BCK18;
		Map<String,Object> m1 = (Map<String,Object>)doctororderupdateDao.queryToObject("Doctororderupdate.getDrugBBY25_002",map);
		Integer BBY19 = 1;
		String BCH01 = "";
		if(m1!=null){
			BBY19 = MapUtils.getInteger(m1,"bby19");
			BCH01 = MapUtils.getString(m1,"bch01");
		}
		Integer BBY25 = 0;
		Integer ACV10 = 9;
		String aPara1 = (String)doctororderupdateDao.queryToObject("Doctororderupdate.getDrugBBY25_003",map);
		Date nowdate = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		map.put("BCK18",BCK18);
		map.put("BBY19",BBY19);
		map.put("BCH01",BCH01);
		map.put("BBY25",BBY25);
		map.put("ACV10",ACV10);
		map.put("aPara1",aPara1);
		Map<String,Object> m2 = null;
		if("1".equals(aPara1)){
			m2 = (Map<String,Object>)doctororderupdateDao.queryToObject("Doctororderupdate.getDrugBBY25_004",map);
		}else{
			m2 = (Map<String,Object>)doctororderupdateDao.queryToObject("Doctororderupdate.getDrugBBY25_005",map);
		}
		Integer cn6 = (Integer)doctororderupdateDao.queryToObject("Doctororderupdate.getDrugBBY25_006",map);
		if(cn6>0){
			Integer cn7 = (Integer)doctororderupdateDao.queryToObject("Doctororderupdate.getDrugBBY25_007",map);
			if(cn7!=null){
				ACV10 = cn7;
			}else{
				Integer cn8 = (Integer)doctororderupdateDao.queryToObject("Doctororderupdate.getDrugBBY25_008",map);
				if(cn8!=null){
					ACV10 = cn8;
				}else {
					ACV10 = 0;
				}
			}
			if(ACV10==0||ACV10==1){
				BCK18 = ACV10;
				map.put("BCK18",BCK18);
			}
			map.put("ACV10",ACV10);
			if((BBY19==0 && BCK18 == 0)||(m2==null)){
				Integer DSK01 = MapUtils.getInteger(m2,"dsk01");
				map.put("DSK01",DSK01);
				resultMap = (Map<String,Object> )doctororderupdateDao.queryToObject("Doctororderupdate.getDrugBBY25_009",map);
			}else{
				resultMap = (Map<String,Object> )doctororderupdateDao.queryToObject("Doctororderupdate.getDrugBBY25_010",map);
			}
		}
		return resultMap;
	}
	/* scriptId(1755) 西成药/中草药计价信息 */
	public ModelVo selectMedFeeDetail(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		String aDate = map.get("aDate").toString()+" 23:59:59.999";
		map.put("aDate", aDate);
		String ABC02 = getABC02(map);
		map.put("ABC02", ABC02);
		Map<String,Object> rlist = getDrugBBY25(map);
		Integer BBY25 = null;
		if(rlist!=null&&rlist.size()>0){
			BBY25 = MapUtils.getInteger(rlist,"bby25");
		}
		map.put("BBY25",BBY25);
		Map<String,Object> m3 = (Map<String,Object> )doctororderupdateDao.queryToObject("Doctororderupdate.getDrugBBY25_011",map);
		String BBY08 = "";
		Double BAG057 = 0.0000;
		if(m3!=null&&m3.size()>0){
			BBY25 = MapUtils.getInteger(m3,"bby25");
			BBY08 = MapUtils.getString(m3,"bby08");
			BAG057 = MapUtils.getDouble(m3,"bag057");
			map.put("BBY25",BBY25);
			map.put("BBY08",BBY08);
			map.put("BAG057",BAG057);
			map.put("aDate",new Date());
			mo =  packModelVo("Doctororderupdate.getDrugBBY25_012",map);
		}
		return mo;
	}
	public ModelVo selectTreatWesternDetail(Map map){
		return packModelVo("Doctororderupdate.1110",map);
	}
	public ModelVo selecttTreatDetail(Map map){
		return packModelVo("Doctororderupdate.1111",map);
	}
	public ModelVo selectHygiMater(Map map){
		return packModelVo("Doctororderupdate.7325",map);
	}
	//获取手术人员
	public ModelVo getoperatingCrew(Map map){
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = doctororderupdateDao.getoperatingCrew(map);
		} catch (Exception e) {
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}

		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);

		mo.setRows(list);
		return mo;
	}
	//更新医嘱为未编辑状态
	public ModelVo updateEdit(Map map)throws Exception{
		ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS, "更新成功!");
		doctororderupdateDao.update("Doctororderupdate.updateEdit",map);
		return mo;
	}
}
